package com.liulishuo.filedownloader;

import com.liulishuo.filedownloader.model.FileDownloadStatus;
import com.waterstudio.filedownloader.ext.m3u8.M3u8DownloadCallback;

import javax.Constant;

/**
 * 作为一个分组描述的下载任务，任务状态需要根据整个分组状态来决定
 * <p>
 * Created by chensuilun on 2019/3/15.
 */
public class M3U8DownloadTask extends DownloadTask {

    private volatile byte mGroupStatus = FileDownloadStatus.INVALID_STATUS;
    private M3u8DownloadCallback mDownloadCallback;

    public M3U8DownloadTask(String url, M3u8DownloadCallback downloadCallback) {
        super(url);
        mDownloadCallback = downloadCallback;
        addHeader(Constant.HEADER_UA, Constant.UA);
        //https://github.com/lingochamp/FileDownloader/issues/1051
        addHeader(Constant.HEADER_ENCODE, Constant.IDENTITY);
    }

    @Override
    public BaseDownloadTask setListener(FileDownloadListener listener) {
        if (listener instanceof M3u8DownloadListenerWrapper) {
            return super.setListener(listener);
        } else if (listener instanceof FileDownloadLargeFileListener) {
            return super.setListener(new M3u8DownloadListenerWrapper((FileDownloadLargeFileListener) listener, mDownloadCallback));
        }
        throw new IllegalStateException(String.format("%s is not suitable", listener.getClass()));
    }

    public byte getGroupStatus() {
        return mGroupStatus;
    }

    public void setGroupStatus(byte groupStatus) {
        mGroupStatus = groupStatus;
    }
}
